let express = require('express')
let app = express()
let db = require('./index')
let selectAll = require('./index')
let router = require('./routes/user')


var multer=require('multer');
var fs=require('fs');

// 那个网页可以跑
app.all('*', function (req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    next();
});

app.use('/index',router)


app.get('/user', (req, res) => {
    let sql = 'SELECT  * from users';
    selectAll(sql, res)

})

var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        // 接收到文件后输出的保存路径（若不存在则需要创建）
        cb(null, 'public/images');
    },
    filename: function (req, file, cb) {
        // 将保存文件名设置为 时间戳 + 文件原始名，比如 151342376785-123.jpg
        cb(null, Date.now() + "-" + file.originalname);
    }
});

var upload = multer({ storage: storage });

app.post('/images',upload.single('file'), (req, res) => {
    var file = req.file;
    console.log('文件类型：%s', file.mimetype);
    console.log('原始文件名：%s', file.originalname);
    console.log('文件大小：%s', file.size);
    console.log('文件保存路径：%s', file.path);
    // 接收文件成功后返回数据给前端
    res.json({ res_code: '0', name: file.originalname, url: file.path });

})

app.listen('8085', function () {
    console.log('running')
})